//
//  MBDUserReportViewController.swift
//  MasterBoy
//
//  Created by kyson on 2023/10/26.
//

import Foundation
import UIKit
import SnapKit
import Toast_Swift
import Dispatch

class MBDUserReportViewController : MBDBaseViewController {
    
    var tableView: UITableView!
    var viewModel : MBDUserReportViewModel!
    private var confirmButton : UIButton!
    static let confirmButtonHeight : CGFloat = 40

    init(viewModel: MBDUserReportViewModel!) {
        super.init(nibName: nil, bundle: nil)
        self.viewModel = viewModel
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "投诉"
        self.view.backgroundColor = UIColor(colorHex: 0xF4F4F4)

        tableView = UITableView()
        
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.allowsMultipleSelection = false
        tableView.allowsSelectionDuringEditing = false
        tableView.allowsMultipleSelectionDuringEditing = false
        self.view.addSubview(tableView)
        
        tableView.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)
        tableView.backgroundColor = UIColor(colorHex: 0xF4F4F4)
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
        tableView.dataSource = self
        tableView.delegate = self
        tableView.rowHeight = 100
        tableView.register(MBDUserReportCell.self, forCellReuseIdentifier: "MBDUserReportCell")

        confirmButton = UIButton()
        self.view.addSubview(confirmButton)
        confirmButton.isEnabled = false
        confirmButton.setTitle("提交", for: .normal)
        confirmButton.layer.cornerRadius = MBDUserReportViewController.confirmButtonHeight / 2.0
        confirmButton.clipsToBounds = true
        confirmButton.setBackgroundColor(color: UIColor(colorHex: 0x4273d0), forState: .normal)
        confirmButton.addTarget(self, action: #selector(confirmButtonClicked), for: .touchUpInside)
        
        tableView.snp.makeConstraints { make in
            make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom).offset(-55)
            make.left.equalTo(self.view)
            make.right.equalTo(self.view)
        }
        
        confirmButton.snp.makeConstraints { make in
            make.bottom.equalTo(self.view.safeAreaLayoutGuide.snp.bottom).offset(-7.5)
            make.centerX.equalTo(self.view)
            make.width.equalTo(200)
            make.height.equalTo(MBDUserReportViewController.confirmButtonHeight)
        }
        
    }
    
    @objc func confirmButtonClicked(){
        self.viewModel.doRequest { error, result in
            if error == nil {
                self.view.makeToast("投诉成功，我们将及时进行审核",position: .center)
                
                DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
                    self.navigationController?.popViewController(animated: true)
                }
            }
        }
    }
}


extension MBDUserReportViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return self.viewModel.cellModels?.count ?? 0
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellModel = self.viewModel.cellModels![indexPath.row]
        
        if cellModel is MBDUserReportCellModel {
            let cell = tableView.dequeueReusableCell(withIdentifier: "MBDUserReportCell", for: indexPath)
            let realCell = cell as! MBDUserReportCell
            realCell.cellModel = cellModel
            return cell
        }
        
        return UITableViewCell()
    }
}


extension MBDUserReportViewController : UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        let cellModel = self.viewModel.cellModels![indexPath.row]
        self.confirmButton.isEnabled = true
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let cellModel = self.viewModel.cellModels![indexPath.row] as! MBDUserReportCellModel

        return cellModel.cellHeight()
    }
}
